package com.dao;

import com.po.Activity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface ActivityRepository extends JpaRepository<Activity, Long>, JpaSpecificationExecutor<Activity>{
    List<Activity> findAllByState(int i);

    @Query(value = "select * from o_activity where activity_state=1",nativeQuery = true)
    Page<Activity> findAll(Pageable pageable);

    @Query(value = "select * from o_activity where activity_state=0",nativeQuery = true)
    Page<Activity> findAllNActivity(Pageable pageable);

    Activity findAllById(Long id);

    @Query(value = "SELECT a.* FROM o_activity a,o_user u,o_activity_users au WHERE a.id=au.activities_id AND u.id=au.users_id AND u.id=?",nativeQuery = true)
    List<Activity> findActivityById(Long id);

    @Transactional
    @Modifying
    @Query(value = "insert into o_activity_users(activities_id,users_id) VALUES(?1,?2)",nativeQuery = true)
    int saveMangToMang(Long id, Long user_id);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM o_activity_users WHERE activities_id = ?1 and users_id=?2",nativeQuery = true)
    int deleteM(Long id, Long u_id);

    @Query(value = "select * from o_activity where type_id=? and activity_state=1",nativeQuery = true)
    List<Activity> findActivityByTypeId(Long id);

    @Query(value = "select * from o_activity where activity_state=1",nativeQuery = true)
    List<Activity> findTop(Pageable pageable);


    @Query(value = "select * from o_activity where activity_state=2",nativeQuery = true)
    Page<Activity> findAllByEnd(Pageable pageable);
}
